package cn.kin.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.kin.pojo.Class;
import cn.kin.pojo.Timetables;
import cn.kin.service.ClassService;
import cn.kin.service.TimetablesService;

@Controller
@RequestMapping("/timetables")
public class TimetablesController {
	public TimetablesController() {
		System.out.println("================");
	}
	@Resource
	TimetablesService tService;
	
	//根据班级，本周/下周进如入查看课表
	@RequestMapping("detail")
	public String detail(@RequestParam("classID")Integer classID,@RequestParam("next")Integer next,Model model) {
		model.addAttribute("next",next);
		model.addAttribute("classID",classID);
		return "/leader/timetablesUpdate";
	}
	
	//获取课表
	@RequestMapping("/list")
	@ResponseBody
	public Object list(@RequestParam(value="classID")Integer classID,@RequestParam(value="next")Integer next) {
		List<Timetables> list=null;
		if(next!=null&&next<0) {
			next=null;
		}
		list=tService.findByClassID(classID, next);
		return list;
	}
	
	//更新课表
	@RequestMapping("/update")
	public String update(@RequestParam(value="classID",required=false)Integer classID,@RequestParam(value="next",required=false)Integer next,
			@RequestParam(value="id",required=false)Integer[] id,
			@RequestParam(value="lessonOne",required=false)String[] lessonOne,
			@RequestParam(value="lessonTwo",required=false)String[] lessonTwo,
			@RequestParam(value="lessonThree",required=false)String[] lessonThree,
			@RequestParam(value="lessonFour",required=false)String[] lessonFour
			,Model model) {
		Map map=new HashMap<>();
		List<Timetables> list=new ArrayList<Timetables>();
		for(int i=0;i<id.length;i++) {
			//System.out.println(id[i]+lessonOne[i]+lessonTwo[i]+lessonThree[i]+lessonFour[i]);
			Timetables t=new Timetables();
			t.setId(id[i]);
			t.setLessonOne(lessonOne[i]);
			t.setLessonTwo(lessonTwo[i]);
			t.setLessonThree(lessonThree[i]);
			t.setLessonFour(lessonFour[i]);
			list.add(t);
		}
		if(tService.updateTimetables(list)) {
			model.addAttribute("msg","修改课程表成功！");
			//修改成功，返回班级列表
			return "redirect:/class/list";
		}
		model.addAttribute("msg","修改课程表失败！");
		//修改失败，返回课程表
		return "redirect:/timetables/timetablesUpdate?classID="+classID+"&next="+next;
	}
}
